﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Epam.PhotoGallery.Interfaces.Providers;
using Epam.PhotoGallery.Bussines.Providers;
using Epam.PhotoGallery.Interfaces.DataTransferObjects;
using Epam.PhotoGallery.Site.Models;
using System.Drawing;

namespace Epam.PhotoGallery.Site.Controllers
{
    public class CommunityController : Controller
    {
        #region Constants

        private const string CATEGORY_VIEW_NAME = "Category";
        
        private const string ADD_TOPIC_FORM_VIEW_NAME = "TopicCreation";
        private const string ADD_CATEGORY_FORM_VIEW_NAME = "CategoryCreation";
        
        private const string CURRENT_CATEGORY_TITLE_PARAM_NAME = "currentCategoryName";
        private const string CURRENT_CATEGORY_ID_PARAM_NAME = "currentCategoryID";

        private const string CONTENT_TYPE_IMAGE_PART_NAME = "image";
        
        private const string INDEX_ACTION_NAME = "Index";
        private const string GET_TOPICS_FROM_CATEGORY_ACTION_NAME = "GetTopicsFromCategory";

        #endregion

        #region Fields

        private ICommunityDataProvider _provider;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the data provider.
        /// </summary>
        public ICommunityDataProvider DataProvider
        {
            get
            {
                if (_provider == null)
                {
                    _provider = new CommunityDataProvider();
                }

                return _provider;
            }

        }

        #endregion


        public ViewResult Index()
        {
            Community community = Community.GetModel(DataProvider);

            IEnumerable<Category> categories = community.GetCategories();

            return View(categories);
        }

        public ViewResult GetTopicsFromCategory(string categoryID)
        {
            Community community = Community.GetModel(DataProvider);

            IEnumerable<BaseTopicInfo> topicsInfos = community.GetBaseTopicsInformation(categoryID);

            Category category = community.GetCategory(categoryID);

            ViewData[CURRENT_CATEGORY_TITLE_PARAM_NAME] = category.Title;
            ViewData[CURRENT_CATEGORY_ID_PARAM_NAME] = categoryID;

            return View(CATEGORY_VIEW_NAME,topicsInfos);
        }

        [HttpGet]
        public ViewResult CreateCategory(string categoryID)
        {
            return View(ADD_TOPIC_FORM_VIEW_NAME, model: categoryID);
        }

        [HttpPost]
        public ActionResult CreateCategory(Category category)
        {
            Community community = Community.GetModel(DataProvider);

            community.AddCategory(category);

            return RedirectToAction(INDEX_ACTION_NAME);
        }

        [HttpGet]
        public ViewResult CreateTopic(string categoryID)
        {
            return View(ADD_TOPIC_FORM_VIEW_NAME,model:categoryID);
        }

        [HttpPost]
        public ActionResult CreateTopic(Topic newTopic,string categoryID)
        {
            Community community = Community.GetModel(DataProvider);

            community.AddTopic(newTopic,categoryID);

            return RedirectToAction(GET_TOPICS_FROM_CATEGORY_ACTION_NAME, new { categoryID = categoryID });
        }

        public FileContentResult GetCategoryImage(string categoryID)
        {
            Community community = Community.GetModel(DataProvider);

            Category category = community.GetCategory(categoryID);

            return File(category.Image.ImageData.Content, category.Image.ImageData.MimeType);
        }
   
    }
}
